<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Suppliers extends Model
{

    protected $table = 'yt_suppliers';
	public $timestamps = false;
	protected $fillable = [
        'name',
        'short_name',
        'ibay_sid',
        'sn',
		'contact',
		'phone',
		'fax',
		'company_email',
		'website',
		'level',
		'a1688id',
		'is_need_invoice_detail',
		'po_ids',
		'source_country',
		'source_place',
		'remarks',
		'created_at'
	];

	const Module = '采购';
	const Page = '供应商';

    public static function boot()
    {
        parent::boot();
        static::deleting(function($model) {
            // 操作日志
            dispatch(
                new \App\Jobs\OperationLog(
                    self::Module,
                    self::Page,
                    auth()->user()->id,
                    $path = '',
                    'DELETE',
                    'ID =>'.$model->id,  //删除的描述
                    $model->toArray()
                )
            );
//            throw new \Exception('无法删除');
        });
    }

    public function getPoIdsAttribute($value)
    {
        return explode(',', $value);
    }

    public function setPoIdsAttribute($value)
    {
        //tags 是分类字段名 我的叫tags
        $this->attributes['po_ids'] = implode(',', $value);
    }

	public function loginstock()
	{
		return $this->belongsTo(LogInStock::class,'supplier','sn');
	}

    static function getSupplyCache()
    {
        return Cache::Remember('supplier', config('constants.CACHE_EXPIRES'), function () {
            return self::all()->pluck('name', 'sn')->toArray();
        });
    }

    static function getSupplySnCache()
    {
        return Cache::Remember('supplierSn', config('constants.CACHE_EXPIRES'), function () {
            return self::all()->pluck('sn', 'sn')->toArray();
        });
    }

    static function getSupplyArrCache($index='id')
    {
        return Cache::Remember('supplier_array', config('constants.CACHE_EXPIRES'), function () use($index) {
            return self::all()->pluck('name', $index)->toArray();
        });
    }

    public static function getSupplierName($supplier_no)
    {
        return Cache::Remember('supplierName:' . $supplier_no, config('constants.CACHE_EXPIRES'), function () use ($supplier_no) {
            return self::where(['sn' => $supplier_no])->first(['name']);
        });
    }


    //原产地国家
    static function getCountryByDistinct():array
    {
        return self::query()->select('source_country')->distinct()
            ->pluck('source_country','source_country')->toArray() ?: [] ;
    }

    public static function getSupplierInfoBySn($supplier_no)
    {
        return Cache::Remember('supplierInfo:' . $supplier_no, config('constants.LITTLE_CACHE_EXPIRES'), function () use ($supplier_no) {
            return self::where(['sn' => $supplier_no])->with('suppliersExt')->first();
        });
    }

    /**
     * 查询扩展信息
     */
    public function suppliersExt(){
        return $this->hasMany(SuppliersExt::class, 'supplier_id', 'id');
    }

    /**
     * 根据sn更新供应商信息
     */
    public static function insertInfo($data){
        return self::create($data)->id;
    }

    /**
     * 根据sn更新供应商信息
    */
    public static function updateInfoBySn($sn, $data){
        return self::where('sn', $sn)->update($data);
    }

    public static function getSupplierInfo($supplier_id)
    {
        return Cache::Remember('supplierInfo:' . $supplier_id, config('constants.LITTLE_CACHE_EXPIRES'), function () use ($supplier_id) {
            return self::where(['id' => $supplier_id])->first();
        });
    }


}